<?php
class SiteMapPage extends Page {
	static $db = array(
	   'ExcludedTypes' => 'Varchar(255)',
	   'RootPages' => 'Varchar(255)'
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Content.Main',new TextField('ExcludedTypes',_t('Sitemappage.Excluded','Ausgeschlossene Seitentypen')));
        $fields->addFieldToTab('Root.Content.Main',new TextField('RootPages',_t('Sitemappage.Root','Wurzelseiten')));
		
		return $fields;
	}
	
}
 
class SiteMapPage_Controller extends Page_Controller {
	function init(){
		parent::init();
	}
	
	function SiteMap() {
		$output = '<div class="sitemap">';
		if ($this->ExcludedTypes)
		  $exclude = explode(',',$this->ExcludedTypes);
		else 
		  $exclude = null;
		  
        if ($this->RootPages)
          $roots = explode(',',$this->RootPages);
        else 
          $roots = array(0);

         foreach ($roots AS $id) {
         	$this->createListSitemap(intval($id),$output,$exclude);
         }
		
		
		$output .= '</div>';
		return $output;
	}
	
	
	function createListSitemap($parentId = 0,&$output,$exclude) {
		$excludeFilter = '';
		if ($exclude)
		   $excludeFilter = ' AND ClassName Not in (\''.implode('\',\'',$exclude).'\')';
		$pages = DataObject::get('SiteTree','ParentID='.$parentId .$excludeFilter);
		if ($pages) {
		    $output .= '<ul>';
			foreach ($pages as $page) {
				$link = '&nbsp;';
				if ($page->ShowInMenus)
				    $link = '<a href="'.$page->Link().'" title="'.$page->Title.'">' . $page->MenuTitle.'</a>';
				$output .= '<li>' . $link;
				$this->createListSitemap($page->ID,$output,$exclude);
				$output .= '</li>';
			}
			$output .= '</ul>';
		}

		
	}
	
	function preparePages($page) {
		$children = $page->Children();
		if ($children) {
			foreach ($children AS $child) {
			     	
			}
		}
	}
	
	function createSiteMap($parentId,$ds) {
		$pages = DataObject::get('SiteTree','ParentID='.$parentId);
		$first = true;
		if ($pages) {
			$i=0;
			foreach ($pages AS $page) {
                if ($page == $pages->First())
                    $page->FirstOfLevel = true;
				if ($page == $pages->Last())
				    $page->LastOfLevel = true;
				$ds->push($page);
			    $this->createSiteMap($page->ID,$ds);	
			}
		}
	}
}

?>